package client.management;

/**
 * This class implements an rmi-callback-function. It's sent to a server, where
 * it's invoked to notificate the management client about some events occured on
 * the server.
 * 
 * @version 1.0 vom 13.11.2012
 * @author Distributed Group 111 1026312, 1026309
 */

import java.io.Serializable;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ManagementClientNotifierImpl extends UnicastRemoteObject implements ManagementClientNotifier,
		Serializable {

	private static final long serialVersionUID = -726392310322644945L;
	private List<String> notifications;
	private String state;

	protected ManagementClientNotifierImpl() throws RemoteException {
		super();
		this.state = "hide";
		this.notifications = Collections.synchronizedList(new ArrayList<String>());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * client.management.ManagementClientNotifier#notificate(java.lang.String)
	 */
	@Override
	public void notificate(String notification) throws RemoteException {
		if (state.equals("hide")) {
			if(notifications.size() > 50000) {
				for(int i = 0; i < 1000; i++) {
					notifications.remove(i);
				}
			}
			this.notifications.add(notification);
		}
			
		else
			System.out.println(notification);
	}
	
	public void setState(String state) {
		this.state = state;
	}
	
	public void print() {
		for(String s : this.notifications) {
			System.out.println(s);
		}
		if(notifications.size() == 0) {
			System.out.println("No notifications to print.");
		}
		this.notifications.clear();
	}
}